package com.codingvip.threadLocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author Tony.Han
 * @Created at 2020/4/6 9:31 下午
 * Desc:
 */
public class ThreadLocalTest {
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        for (int i = 0; i < 2; i++) {
            MyThread myThread = new MyThread();
            executorService.execute(myThread);

        }
        try {
            executorService.awaitTermination(60 * 5 * 1000, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static class MyThread extends Thread {
        private static boolean flag = true;
        @Override
        public void run() {
            if (flag) {
                threadLocal.set(this.getName() + ".SessionInfo Set");
                flag = false;
            }

            System.out.println(this.getName() + "线程内容是：：：：" + threadLocal.get());
            threadLocal.remove();

        }
    }
}
